/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project2;

import java.util.HashMap;

/**
 *
 * @author asdf
 */
public class SequencesConstraint extends Constraint {

    private int[] assignments;

    @Override
    public boolean check(int[] assignments, int variableIndex, int valueIndex) {
        this.assignments = assignments;

        int[][] seq2=SetUp.getInstance().getNotAllowedSequences2();
        int[][] seq3=SetUp.getInstance().getNotAllowedSequences3();
        
        //check 2-seqs
        for(int i=0;i<seq2.length;i++){
            if(valueIndex==seq2[i][0]&&isValueAssignedAt(seq2[i][1],variableIndex+1))
                    return false;
            if(valueIndex==seq2[i][1]&&isValueAssignedAt(seq2[i][0],variableIndex-1))
                    return false;
        }
        
        //check 3-seqs
        for(int i=0;i<seq3.length;i++){
            if(valueIndex==seq3[i][0]&&isValueAssignedAt(seq3[i][1],variableIndex+1)&&isValueAssignedAt(seq3[i][2],variableIndex+2))
                    return false;
            if(valueIndex==seq3[i][1]&&isValueAssignedAt(seq3[i][0],variableIndex-1)&&isValueAssignedAt(seq3[i][2],variableIndex+1))
                    return false;
            if(valueIndex==seq3[i][2]&&isValueAssignedAt(seq3[i][0],variableIndex-2)&&isValueAssignedAt(seq3[i][1],variableIndex-1))
                    return false;
            
        }
              
        
        return true;
    }

    private boolean isValueAssignedAt(int value, int index) {
        return assignments[wrapIndex(index)] == value;
    }

    private int wrapIndex(int index) {
        int n = SetUp.getInstance().getLengthOfSchedule() * SetUp.getInstance().getNumberOfEmployees();
        if (index < 0) {
            index += n;
        } else {
            index = index % n;
        }

        return index;
    }
}
